//
//  NSString+MD5.m
//  easystudy
//
//  Created by kane on 15/6/5.
//  Copyright (c) 2015年 kane. All rights reserved.
//

#import "NSString+MD5.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5)

- (NSString *)MD5Encoding
{
    const char *cStr = [self UTF8String];
    unsigned char result[16];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    return [NSString stringWithFormat:
            @"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
            result[0], result[1], result[2], result[3],
            result[4], result[5], result[6], result[7],
            result[8], result[9], result[10], result[11],
            result[12], result[13], result[14], result[15]
            ];
}

- (NSString*)indexs
{
    if (!self || [self length] == 0) {
        return @"*";
    }
    //    NSString *preString =[NSString stringWithFormat:@"%@",self];
    //
    //    if ([[preString substringToIndex:1] compare:@"长"] ==NSOrderedSame)
    //    {
    //        return @"C";
    //    }
    //    if ([[preString substringToIndex:1] compare:@"沈"] ==NSOrderedSame)
    //    {
    //        return @"S";
    //    }
    //
    //    if ([[preString substringToIndex:1] compare:@"厦"] ==NSOrderedSame)
    //    {
    //        return @"X";
    //    }
    //
    //    if ([[preString substringToIndex:1] compare:@"地"] ==NSOrderedSame)
    //    {
    //        return @"D";
    //    }
    //
    //    if ([[preString substringToIndex:1] compare:@"重"] ==NSOrderedSame)
    //    {
    //        return @"C";
    //    }
    
    NSMutableString *ms = [[NSMutableString alloc] initWithString:self];
    CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformMandarinLatin, NO);
    if (CFStringTransform((__bridge CFMutableStringRef)ms, 0, kCFStringTransformStripDiacritics, NO)) {
        NSString *first = [[ms substringToIndex:1] uppercaseString];
        return first;
    }
    return @"";
}
@end
